#!/usr/bin/python

import re

# Buildings on North Campus
# mcc: XXX put buildings and codes in a file and create dict from there
buildings = {"A&AB":"Art & Architecture Building", \
        "AL":"Walter E. Lay Automotive Lab", \
        "BAM HALL":"Blach Anderson Moore Hall, School of Music", \
        "CHRYS":"Chrysler Center", \
        "COOL":"Cooley Building", \
        "CSE":"Computer Science & Engineering Building", \
        "DC":"Duderstadt Center", \
        "DOW":"Dow Engineering Building", \
        "EECS":"Electrical Engineering and Computer Science Building", \
        "EPB":"Engineering Program Building", \
        "ERB1":"Engineering Research Building 1", \
        "ERB2":"Engineering Research Building 2", \
        "EWRE":"Environmental & Water Resources Engineering Building", \
        "FXB":"Francois-Xavier Bagnoud Building", \
        "GGBL":"G.G. Brown Laboratory", \
        "IOE":"Industrial and Operations Engineering Building", \
        "LBME":"Lurie Biomedical Engineering Building", \
        "LEC":"Lurie Engineering Center", \
        "NAME":"Naval Architecture and Marine Engineering Building", \
        "NCRB":"North Campus Recreation Building", \
        "PHOENIXLAB":"Phoenix Memorial Laboratory", \
        "PIER":"Pierpont Commons", \
        "SM":"Earl V. Moore Building, School of Music", \
        "SRB":"Space Research Building", \
        "STAMPS":"Stamps Auditorium", \
        "STRNS":"Sterns Building", \
        "WDC":"Charles R. Walgreen, Jr. Drama Center"}

def location_of_food(email_string, buildings, building_abbrv=False):
    email_string = re.split('\n',email_string)
    location = __location_finder(email_string)
    # mcc: search for building abbreviations in location string
    if building_abbrv:
        complete_location = ""
        buildings_regex = {}
        for key in buildings:
            building_regex = re.compile(key)
            buildings_regex[key] = building_regex
        for key in buildings_regex:
            if re.match(buildings_regex[key], location) is not None:
                complete_location = location.replace(key, buildings[key])
        return complete_location
    else:
        return location

def remove_time(string):
    # XXX mcc: Some emails have something like FOOD - TIME - PLACE or
    # FOOD - PLACE - TIME. I want to strip out the time
    return string

def __location_finder(email_string):
    '''
    Given an email get the location
    '''
    # search each line of the email for certain regex that match common
    # location indicators
    loc_keys = ['Location','Where']
    loc_re = "(?:"
    for l in loc_keys:
        loc_re += "(?<=^%s:)|(?<=^%s :)|" % (l,l)
    if loc_re[-1]=='|': loc_re=loc_re[:-1]
    loc_re += ").*"
    #print loc_re
    for line in email_string:
        # clean line of white space
        clean_line = line.lstrip()
        clean_line = line.rstrip()
        # match "Location:"
        #regex_match = re.search("(?:(?<=^Location:)|(?<=^Location :)).*",clean_line,re.I)
        regex_match = re.search(loc_re,clean_line,re.I)
        if regex_match is not None:
            return regex_match.group(0).lstrip().rstrip()
        # match Subject: [tea|food] @
        regex_pattern = re.compile("^Subject:.*[tea|food].*@",re.I)
        regex_match = re.match(regex_pattern, clean_line)
        if regex_match is not None:
            split = re.split(regex_pattern, clean_line)
            assert len(split) > 1
            return split[1].lstrip().rstrip()
    return ""
